package com.ruyuan.user.server.controller.converter;

import com.ruyuan.common.model.Features;
import com.ruyuan.gateway.sdk.facade.RuyuanGatewayFacade;
import com.ruyuan.gateway.sdk.model.UserAccountInfo;
import com.ruyuan.user.api.model.constants.UserAccountInfoExtendKeys;
import com.ruyuan.user.server.controller.request.SaveRoleRequest;
import com.ruyuan.user.server.controller.response.RoleListVO;
import com.ruyuan.user.server.domain.model.Role;
import com.ruyuan.user.server.repository.bean.RoleDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author xx
 */
@Component
public class WebRoleConverter {

    @Autowired
    private RuyuanGatewayFacade ruyuanGatewayFacade;

    public Role convertSaveRequestToModel(SaveRoleRequest saveRoleRequest) {
        UserAccountInfo currentUser = ruyuanGatewayFacade.getCurrentUser();
        Long orgId = Long.parseLong(currentUser.getExtendInfo().get(UserAccountInfoExtendKeys.ROOT_ORG_ID));
        Role role = new Role();
        BeanUtils.copyProperties(saveRoleRequest, role);
        if (role.getDescription() == null) {
            // 传null设置为空字符串, 覆盖DB字段
            role.setDescription("");
        }
        role.setModifier(String.valueOf(currentUser.getUserId()));
        role.setId(saveRoleRequest.getRoleId());
        role.setOrgId(orgId);
        role.setIsDeleted(false);
        role.setFeatures(Features.of());
        return role;
    }

    public RoleListVO convertModelToListVO(Role role) {
        if (role == null) {
            return null;
        }
        RoleListVO roleListVO = new RoleListVO();
        BeanUtils.copyProperties(role, roleListVO);
        roleListVO.setRoleId(role.getId());
        return roleListVO;
    }

}
